package com.buddy.learn.spring.framework.webmvc.servlet;

import java.io.File;
import java.util.Locale;

public class BViewResolver {

    private final String DEFAULT_TEMPLATE_SUFFX = ".html";
    private File templateRootDir;

    public BViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        this.templateRootDir = new File(templateRoot);
    }

    /**
     * local用来做国际化的
     *
     * @param viewName
     * @param locale
     * @return
     * @throws Exception
     */
    public BView resolveViewName(String viewName, Locale locale) throws Exception {

        if (null == viewName || "".equals(viewName)) {
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFX) ? viewName : viewName + DEFAULT_TEMPLATE_SUFFX;
        File templateFile = new File((templateRootDir.getParent() + "/" + viewName).replaceAll("/+", "/"));
        return new BView(templateFile);
    }
}
